<?php
namespace app\common\model;
use think\Model;
class Category extends Model{
    protected  $autoWriteTimestamp=true;
    public function add($data){
       
        $data['status']=1;
       // $data['create_time']=time();
        return $this->save($data);
    }
    //获取所有的分类信息
    public function getNormalFirstCategory(){
        $data=[
            'status'=>1,//    所有的一级分类
            'parent_id'=>0,
        ];
        //倒序
        $order=[
            'id'=>'desc'
        ];
        
       return  $this->where($data)->order($order)->select();
        
    }
    //获取分类信息
    public function getFirstCategorys($parentId=0){
        $data=[
            'status'=>['neq',-1],  // 除了删除情况外
            'parent_id'=>$parentId,
        ];
        //倒序
        $order=[
            'listorder'=>'desc',
            'id'=>'desc'
        ];
        
        $result=$this->where($data)
                ->order($order)
                ->paginate(5);
        // echo $this->getLastSql();
         return $result;
    }
    //子分类select
    public function getNormalFirstCategorys($parentId=0){
        $data=[
            'status'=>['neq',-1],//除了删除情况外
            'parent_id'=>$parentId,
        ];
        //倒序
        $order=[
            'listorder'=>'desc',
            'id'=>'desc'
        ];
        
        $result=$this->where($data)
                ->order($order)
                ->select();
        // echo $this->getLastSql();
         return $result;
    }
    
       public function getNormalCategoryByParentId($parentId=0) {
        $data = [
            'status' => 1,//正常情况下
            'parent_id' => $parentId,
        ];

        $order = [
            'id' => 'desc',
        ];

        return $this->where($data)
            ->order($order)
            ->select();
    }
    
    public function getNormalRecommendCategoryByParentId($id=0, $limit=5) {
        $data = [
            'parent_id' => $id,
            'status' => 1,
        ];

        $order = [
            'listorder' => 'desc',
            'id' => 'desc',
        ];

        $result = $this->where($data)
            ->order($order);
        if($limit) {
            $result = $result->limit($limit);
        }

        return $result->select();

    }
    //根据category的所有的parent_id来获取其下面的所有分类
    public function getNormalCategoryIdParentId($ids) {
        $data = [
            'parent_id' => ['in', implode(',', $ids)],
            'status' => 1,
        ];

        $order = [
            'listorder' => 'desc',
            'id' => 'desc',
        ];

        $result = $this->where($data)
            ->order($order)
            ->select();

        return $result;
    }
    
}
